/* global  expect, apiTest */

require('../../apiTest')
const Staff = {}

// 列表单条验证
Staff.checkOne = function(v, post) {
  expect(v).to.be.an('object')
  apiTest.isObjectId(v._id)
  expect(v.name).to.be.a('string')
  if (v.oaid) {
    expect(v.oaid).to.be.an('string')
    ;['name', 'oaid', 'no', 'mobile', 'department', 'post', 'status', 'type'].forEach((key) => {
      expect(v[key]).to.be.a('string', '属性: "' + key + '" 类型错误!')
    })
    // 可选项判断
    ;['gender'].forEach((key) => {
      if (v[key]) expect(v[key]).to.equal(v[key], '属性: "' + key + '" 类型错误!')
    })
  }
  if (v.powers) expect(v.powers).to.be.an('array')
  if (post) {
    ['name', 'no', 'gender', 'mobile', 'idcard', 'department', 'post', 'status', 'type'].forEach((key) => {
      if (post[key]) expect(v[key]).to.equal(post[key], '属性: "' + key + '" 不一致!')
    })
  }
}

// 列表通用验证
Staff.checkList = function(get, cb) {
  return apiTest.get('/staff/find', Object.assign({}, get))
    .then(function(res) {
      var data = apiTest.apiOk(res)
      expect(data).to.be.a('object')
      // 验证分页
      apiTest.checkPaging(data, get)
      data.list.forEach(function(v) {
        Staff.checkOne(v)
        if (cb) cb(v)
      })
    })
}
// 生成随机提交数据
Staff.randOne = function() {
  return {
    oaid: 'oaid' + (new Date()).getTime(),
    no: '' + (new Date()).getTime(),
    password: apiTest.random(10000000, 99999999) + '',
    name: 'autoTest' + (new Date()).getTime(),
    gender: ['male', 'female'][apiTest.random(0, 1)],
    mobile: '138' + apiTest.random(10000000, 99999999),
    idcard: '' + apiTest.random(100000, 999999) + apiTest.random(1988, 2010) + apiTest.random(10000000, 99999999),
    department: ['16', '17'][apiTest.random(0, 1)],
    post: ['工程师', '经理'][apiTest.random(0, 1)],
    status: ['tryout', 'regular', 'leave', 'dismiss', 'loan', 'retired'][apiTest.random(0, 5)],
    type: ['full', 'part', 'intern', 'rehire'][apiTest.random(0, 3)]
  }
}

// 删除角色
Staff.removeOne = function(_id, cb) {
  return apiTest.post('/staff/remove', { _id: _id }).then(function(res) {
    var data = apiTest.apiOk(res)
    expect(data.ok).to.equal(1)
    expect(data.n).to.equal(1)
    if (cb) return cb()
  })
}
// 修改并查询验证
Staff.editOne = function(_id, oaid, cb) {
  var post = Staff.randOne()
  // post.oaid = oaid
  return apiTest.post('/api/staff/update?oaid=' + oaid, post, 'form').then(function(res) {
    apiTest.apiOk(res)
    // 查
    return apiTest.get('/staff/findOne', { _id: _id }).then(function(res) {
      var data = apiTest.apiOk(res)
      Staff.checkOne(data, post)
      expect(data.oaid).to.equal(oaid)
      if (cb) return cb(data, post)
    })
  })
}
// 修改密码并查询验证
Staff.passwdOne = function(_id, oaid, cb) {
  var post = { password: '' + apiTest.random(10000000, 99999999) }
  // post.oaid = oaid
  return apiTest.post('/api/staff/update_passwd?oaid=' + oaid, post, 'form').then(function(res) {
    apiTest.apiOk(res)
    // 查
    return apiTest.get('/staff/findOne', { _id: _id }).then(function(res) {
      var data = apiTest.apiOk(res)
      Staff.checkOne(data, post)
      // expect(data.password).to.equal(post.password)
      expect(data.oaid).to.equal(oaid)
      if (cb) return cb(data, post)
    })
  })
}

// 添加
Staff.addOne = function(post, cb) {
  return apiTest.post('/api/staff/create', post, 'form').then(function(res) {
    var data = apiTest.apiOk(res)
    Staff.checkOne(data, post)
    expect(data.oaid).to.equal(post.oaid)
    if (cb) return cb(data)
  })
}

module.exports = Staff
